<?php
error_reporting(0);
ini_set('memory_limit', '1000M');
set_time_limit(0);


/** 
 *  mapper_7.php
 *      input:格式：mapred3输出 (json1\tjson2\t...)尤其要注意其中的weight字段
		1.对比上 	 weight>=35
		5.仅模糊对比上   1<=weight<35
		7.独立房源       weight=0   
		8.异常数据    （第一轮处理）
 *      handle:生成sql
 *      output:sql语句--INSERT INTO `house_rent_gov` VALUES ('1', ...);
 * @created 2016-05-12
 * @by lily
 */
$nu=0;
while (($line_json = fgets(STDIN)) !== false) {   //line_json=>source_url\tjson格式
    
    $line_json = trim($line_json);
    if (empty($line_json))
        continue;
	$aa=explode("\t", $line_json);
    //list ($unique_id,$min_price, $cnt, $deviation, $list_json) = explode("\t", $line_json);
	for($i=6;$i<count($aa);$i++){
		if($nu==0) {
			echo "INSERT INTO house_rent_gov_new(id,house_title,cityarea_id,cityarea2_id,borough_name,borough_id,house_totalarea,house_price,house_toward,house_room,house_hall,house_toilet,house_kitchen,house_fitment,house_type,house_relet,house_floor,house_topfloor,owner_name,owner_phone,service_phone,house_desc,house_pic_unit,house_pic_layout,source,company_id,source_owner,app_url,wap_url,source_url,sex,into_house,pay_type,pay_method,unique_key,tag,comment,house_number,deposit,house_configroom,house_configpub,is_ture,created,updated,is_fill,source_name,is_contrast,chain_url) VALUES";
			$nu=1;
		}  
		else{
			echo ",\n";
			$nu+=1;
		} 
		
		$one_dejson=json_decode($aa[$i],true); 
		
		if($aa[2]>1) $is_contrast=1;
		//elseif($one_dejson['weight']>=1&&$one_dejson['weight']<35) $is_contrast=5;  //权重小于35
		elseif($one_dejson['source']=='10'&&
			($one_dejson['house_floor'] == "0" || $one_dejson['house_topfloor'] == "0" || 
			$one_dejson['cityarea_id'] == ''))  $is_contrast=8;
		else $is_contrast=7;  //独立房源
		
		echo " ('".$one_dejson['id']."','".
			str_replace("'","",str_replace("\\","",$one_dejson['house_title']))."','".
			$one_dejson['cityarea_id']."','".$one_dejson['cityarea2_id']."','".
			$one_dejson['borough_name']."','".$one_dejson['borough_id']."','".
			$one_dejson['house_totalarea']."','".$one_dejson['house_price']."','".
			$one_dejson['house_toward']."','".$one_dejson['house_room']."','".
			$one_dejson['house_hall']."','".$one_dejson['house_toilet']."','".
			$one_dejson['house_kitchen']."','".$one_dejson['house_fitment']."','".
			$one_dejson['house_type']."','".$one_dejson['house_relet']."','".
			$one_dejson['house_floor']."','".$one_dejson['house_topfloor']."','".
			str_replace("'","",$one_dejson['owner_name'])."','".
			$one_dejson['owner_phone']."','".$one_dejson['service_phone']."','".
			str_replace("'","",str_replace("\\","",$one_dejson['house_desc']))."','".
			$one_dejson['house_pic_unit']."','".$one_dejson['house_pic_layout']."','".
			$one_dejson['source']."','".$one_dejson['company_id']."','".
            $one_dejson['source_owner']."','".
			$one_dejson['app_url']."','".
			$one_dejson['wap_url']."','".$one_dejson['source_url']."','".
			$one_dejson['sex']."','".$one_dejson['into_house']."','".
			$one_dejson['pay_type']."','".$one_dejson['pay_method']."','".
            $one_dejson['unique_key']."','".
			$one_dejson['tag']."','".$one_dejson['comment']."','".
			$one_dejson['house_number']."','".$one_dejson['deposit']."','".
			$one_dejson['house_configroom']."','".$one_dejson['house_configpub']."','".
			$one_dejson['is_ture']."','".$one_dejson['created']."','".
			$one_dejson['updated']."','".$one_dejson['is_fill']."','".
			$one_dejson['source_name']."','".
			$is_contrast."','".$one_dejson['chain_url']."')";	
		if($nu==19){
		echo ";\n";
		$nu=0;
		}
	} 	
}
echo ";\n";
?>

